<?php

namespace ctrl;

use model\applinks;
use libs\view;

class applink extends base
{
	private static $M;
	public static function Init()
	{
		parent::init();
		self::$M = new applinks;
	}

	public static function index()
	{
		if (isset($_GET['list_json'])) {
			jsonList(self::$M->jsonPageSelect());
		} else {
			view::assign('catelist', self::$M->SelectDataCate());
			view::assign('cid', $_GET['cid'] ?? '');
			view::display("index_applink");
		}
	}

	public static function add()
	{
		if (isset($_GET['view'])) {
			view::assign('catelist', self::$M->SelectDataCate());
			view::assign('applink', null);
			view::assign('title', "添加应用链接");
			view::display("post_applink");
		} else {
			$result = self::$M->insertData();
			Response(array('status' => $result['status'], 'info' => $result['msg']));
		}
	}


	public static function edit()
	{
		if (isset($_GET['view'])) {
			view::assign('applink', self::$M->findData());
			view::assign('catelist', self::$M->SelectDataCate());
			view::assign('title', "编辑应用链接");
			view::display("post_applink");
		} elseif (isset($_GET['sortedit'])) {
			$result = self::$M->updateSort();
			Response(array('status' => $result['status'], 'info' => $result['msg']));
		} elseif (isset($_GET['switchStatus'])) {
			if (self::$M->switchStatus())  Response(array('status' => 1, 'info' => '设置成功'));
		} else {
			$result = self::$M->updateData();
			Response(array('status' => $result['status'], 'info' => $result['msg']));
		}
	}

	public static function del()
	{
		if (self::$M->deleteOneData())
			Response(array('status' => 1, 'info' => '删除成功'));
		else
			Response(array('status' => 0, 'info' => '删除错误!'));
	}


	/****
	 * 应用分类
	 * **/

	public static function cateindex()
	{
		if (isset($_GET['list_json'])) {
			jsonList(self::$M->jsonPageSelectCate());
		} else view::display("index_cate");
	}

	public static function cateadd()
	{
		if (isset($_GET['view'])) {
			view::assign('info', null);
			view::assign('title', "添加应用分类");
			view::display("post_cate");
		} else {
			$result = self::$M->insertDataCate();
			Response(array('status' => $result['status'], 'info' => $result['msg']));
		}
	}


	public static function cateedit()
	{
		if (isset($_GET['view'])) {
			view::assign('info', self::$M->findDataCate());
			view::assign('title', "编辑应用分类");
			view::display("post_cate");
		} else {
			$result = self::$M->updateDataCate();
			Response(array('status' => $result['status'], 'info' => $result['msg']));
		}
	}

	public static function catedel()
	{
		if (isset($_POST['id'])) {
			if (self::$M->deleteOneDataCate())
				Response(array('status' => 1, 'info' => '删除成功'));
		} else Response(array('status' => 0, 'info' => 'ID错误!'));
	}
}
